<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<script>
    var ctx = ctx;
    var patientId = '${nurseTempInfo.patientId}';
    var visitId = '${nurseTempInfo.visitId}';
    var id = '${nurseTempInfo.id}';
    $(function () {
        patientId = $("#patientId").val();
        visitId = $("#visitId").val();
        id = $("#id").val();
        loadDiv('detailsDiv', ctx + '/nurse/nurseTempDetailInfo/index?tempInfoId=' + id);
        loadDiv('eventDiv', ctx + '/nurse/nurseVitalSignsRec/index?patientId=' + patientId + '&visitId=' + visitId + '&tempInfo.id=' + id);

        $('#saveBtn').click(function () {
            var itemInfos = [], detailInfos = [];
            // 辅助护理项目数据处理
            $('.item-infos').each(function () {
                var $obj = $(this)
                if ($obj.val() != $obj.data('value')) {
                    var o = {
                        id: $obj.attr('id'),
                        itemValue: $obj.val(),
                        itemDict: {
                            id: $obj.data('itemid')
                        },
                        infoId: id
                    }
                    itemInfos.push(o)
                }
            })
            // 主要护理项目数据处理
            $('#tempDetailTable tbody tr').each(function () {
                var inputs = $('input', this);
                var flag = false;
                var o = {id: $(this).attr('id'), temperatureType: '2'};
                for (var i = 0; i < inputs.length; i++) {
                    if ($(inputs[i]).val() != $(inputs[i]).data('value')) {
                        flag = true;
                    }
                    o[$(inputs[i]).attr('name')] = $(inputs[i]).val();
                }
                flag && detailInfos.push(o)
            })
            if (itemInfos.length == 0 && detailInfos.length == 0) {
                toastr.warning('没有要保存的数据');
                return false;
            }
            $.ajax({
                contentType: "application/json",
                type: 'post', // 提交方式 get/post
                url: '${ctx}/nurse/nurseTempInfo/saveItemInfo', // 需要提交的 url
                data: JSON.stringify({itemInfos: itemInfos, nurseTempDetailInfos: detailInfos}),
                dataType: 'json',
                success: function (data) {
                    data && data.code == 'success' ? toastr.success("保存成功") : toastr.success("保存失败")
                },
                error: function () {
                    toastr.error('网络连接错误,请检查网络');
                }
            })
        })
    });


    /*项目事件*/
    function addEventItem() {
        id = $("#id").val();
        patientId = $("#patientId").val();
        $("#eventDiv").attr("class", " panel col-lg-8");
        $("#eventFormDiv").show('normal');
        loadDiv('eventFormDiv', ctx + '/nurse/nurseVitalSignsRec/form?patientId=' + patientId + '&visitId=' + visitId + "&tempInfo.id=" + id + '&tempItem.nurseGroups=${type}');


    }
    /*保存项目事件*/
    function saveEvent(divId, formId, saveUrl, loadUrl) {
        if (flag) {
            if ($('#' + formId).parsley().validate()) {
                flag = false;
                $.ajax({
                    type: "POST",
                    data: $("#" + formId).serialize(),
                    url: saveUrl,
                    dataType: "json",
                    success: function (data) {
                        if (data.code == "success") {
                            toastr.success("保存成功");
                            closeDiv();
                            loadDiv(divId, loadUrl + '?patientId=' + patientId + '&visitId=' + visitId + '&tempInfo.id=' + $('#id').val());
                        } else {
                            toastr.error(data.data);
                        }
                    },
                    error: function (data) {
                        toastr.error('网络连接错误,请检查网络');
                    }
                });
            }

        }
        return false;

    }

    function closeDiv() {
        $("#eventDiv").attr("class", " panel col-lg-12");
        $("#eventDiv").show('normal');
        $("#eventFormDiv").hide();
    }
</script>
<div class="content-wrap">
    <div class="wrapper" style="bottom: 50px;">
        <ul class="nav nav-tabs">
            <li><a href="#" onclick="loadDiv('mainCenterDiv','${ctx}/nurse/index')">床位列表</a></li>
            <li class="active">
                <a href="#">护理</a>
            </li>
        </ul>
        <section class="panel panel-default">
            <div class="panel-body">
                <div class="table-responsive no-border">
                    <div class="datatable-top">
                        <div class="pull-left">
                            <form:form id="searchForm" modelAttribute="nurseTempInfo"
                                       onsubmit="return loadDivForm('mainCenterDiv','searchForm','${ctx}/nurse/nurseTempInfo/index');"
                                       method="post" class="form-inline">
                                <form:hidden path="patientId"/>
                                <form:hidden path="visitId"/>
                                <form:hidden path="badNo"/>
                                <input type="hidden" name="type" value="${type}"/>
                                <label> 日期：</label><input name="inspecttionDate"
                                                          onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" readonly
                                                          value='<fmt:formatDate value="${nurseTempInfo.inspecttionDate}" pattern="yyyy-MM-dd"/>'
                                                          class="form-control Wdate"/>
                                &nbsp;&nbsp;
                                <input id="btnSearch" class="btn btn-primary" type="submit" value="查询"/>
                                <a href="#" id="saveBtn" class="btn btn-primary">保 存</a>
                                <input onclick="loadDiv('mainCenterDiv', '${ctx}/nurse/nurseTempInfo/generateInfo?patientId=${nurseTempInfo.patientId}&visitId=${nurseTempInfo.visitId}&type=${type}')"
                                       class="btn btn-primary" type="button" value="变化曲线图"/>
                            </form:form>
                        </div>
                    </div>
                    <div class="col-lg-12">
                        <div class="col-lg-12 panel">
                            <div class="col-lg-5">
                                <fieldset style="height: 443px">
                                    <legend>辅助护理项目</legend>
                                    <section class="panel">
                                        <form:form id="inputTempForm" modelAttribute="nurseTempInfo" method="post"
                                                   class="form-horizontal">
                                            <form:hidden path="id" id="id"/>
                                            <c:forEach items="${nurseTempItemInfo}" var="itemInfo" varStatus="status">
                                                <c:if test="${status.index % 2 == 0}">
                                                    <div class="form-group">
                                                </c:if>
                                                <label class="col-sm-2 control-label">${itemInfo.itemDict.vitalSigns}：</label>

                                                <div class="col-sm-4">
                                                    <div class="input-group mb2">
                                                        <input type="text" htmlEscape="false"
                                                               class="form-control item-infos" maxlength="50"
                                                               id="${itemInfo.id}" data-value="${itemInfo.itemValue}"
                                                               data-itemid="${itemInfo.itemDict.id}"
                                                               value="${itemInfo.itemValue}"/>
                                                        <c:if test="${not empty itemInfo.itemDict.unit}">
                                                        <span class="input-group-btn"><button class="btn btn-default"
                                                                                              type="button"
                                                                                              style="width: 50px">${itemInfo.itemDict.unit}</button></span>
                                                        </c:if>
                                                    </div>
                                                </div>
                                                <c:if test="${status.index % 2 == 1 or status.last}">
                                                    </div>
                                                </c:if>
                                            </c:forEach>
                                        </form:form></section>
                                </fieldset>
                            </div>

                            <div class="col-lg-7">
                                <fieldset>
                                    <legend>主要护理项目</legend>
                                    <section class="panel" id="detailsDiv">
                                    </section>
                                </fieldset>
                            </div>


                        </div>
                    </div>

                    <div>
                        <fieldset style="margin: 0 25px 0 25px">
                            <legend>护理事件</legend>
                            <section class="col-lg-12 panel" id="eventDiv"> </section>
                            <section class="col-lg-4 panel" id="eventFormDiv" style="display: none"> </section>
                        </fieldset>
                    </div>
                </div>
            </div>
        </section>
    </div>
</div>





